package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;

import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for MedicinePrice entities
 * 
 */

@Service("MedicinePriceService")

@Transactional
public class MedicinePriceServiceImpl implements MedicinePriceService {

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * Instantiates a new MedicinePriceServiceImpl.
	 *
	 */
	public MedicinePriceServiceImpl() {
	}

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public MedicinePrice deleteMedicinePriceMedicineStorage(Integer medicineprice_id, Integer related_medicinestorage_id) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id, -1, -1);
		MedicineStorage related_medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage_id, -1, -1);

		medicineprice.setMedicineStorage(null);
		related_medicinestorage.getMedicinePrices().remove(medicineprice);
		medicineprice = medicinePriceDAO.store(medicineprice);
		medicinePriceDAO.flush();

		related_medicinestorage = medicineStorageDAO.store(related_medicinestorage);
		medicineStorageDAO.flush();

		medicineStorageDAO.remove(related_medicinestorage);
		medicineStorageDAO.flush();

		return medicineprice;
	}

	/**
	 * Save an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public void saveMedicinePrice(MedicinePrice medicineprice) {
		MedicinePrice existingMedicinePrice = medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice.getId());

		if (existingMedicinePrice != null) {
			if (existingMedicinePrice != medicineprice) {
				existingMedicinePrice.setId(medicineprice.getId());
				existingMedicinePrice.setPrice(medicineprice.getPrice());
			}
			medicineprice = medicinePriceDAO.store(existingMedicinePrice);
		} else {
			medicineprice = medicinePriceDAO.store(medicineprice);
		}
		medicinePriceDAO.flush();
	}

	/**
	 * Delete an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public MedicinePrice deleteMedicinePriceMedicineSupplier(Integer medicineprice_id, Integer related_medicinesupplier_id) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id, -1, -1);
		MedicineSupplier related_medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesupplier_id, -1, -1);

		medicineprice.setMedicineSupplier(null);
		related_medicinesupplier.getMedicinePrices().remove(medicineprice);
		medicineprice = medicinePriceDAO.store(medicineprice);
		medicinePriceDAO.flush();

		related_medicinesupplier = medicineSupplierDAO.store(related_medicinesupplier);
		medicineSupplierDAO.flush();

		medicineSupplierDAO.remove(related_medicinesupplier);
		medicineSupplierDAO.flush();

		return medicineprice;
	}

	/**
	 * Save an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public MedicinePrice saveMedicinePriceMedicineStorage(Integer id, MedicineStorage related_medicinestorage) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(id, -1, -1);
		MedicineStorage existingmedicineStorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineStorage != null) {
			existingmedicineStorage.setId(related_medicinestorage.getId());
			existingmedicineStorage.setTime(related_medicinestorage.getTime());
			related_medicinestorage = existingmedicineStorage;
		} else {
			related_medicinestorage = medicineStorageDAO.store(related_medicinestorage);
			medicineStorageDAO.flush();
		}

		medicineprice.setMedicineStorage(related_medicinestorage);
		related_medicinestorage.getMedicinePrices().add(medicineprice);
		medicineprice = medicinePriceDAO.store(medicineprice);
		medicinePriceDAO.flush();

		related_medicinestorage = medicineStorageDAO.store(related_medicinestorage);
		medicineStorageDAO.flush();

		return medicineprice;
	}

	/**
	 */
	@Transactional
	public MedicinePrice findMedicinePriceByPrimaryKey(Integer id) {
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(id);
	}

	/**
	 * Return a count of all MedicinePrice entity
	 * 
	 */
	@Transactional
	public Integer countMedicinePrices() {
		return ((Long) medicinePriceDAO.createQuerySingleResult("select count(o) from MedicinePrice o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public Set<MedicinePrice> loadMedicinePrices() {
		return medicinePriceDAO.findAllMedicinePrices();
	}

	/**
	 * Return all MedicinePrice entity
	 * 
	 */
	@Transactional
	public List<MedicinePrice> findAllMedicinePrices(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<MedicinePrice>(medicinePriceDAO.findAllMedicinePrices(startResult, maxRows));
	}

	/**
	 * Delete an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public void deleteMedicinePrice(MedicinePrice medicineprice) {
		medicinePriceDAO.remove(medicineprice);
		medicinePriceDAO.flush();
	}

	/**
	 * Save an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public MedicinePrice saveMedicinePriceMedicineSupplier(Integer id, MedicineSupplier related_medicinesupplier) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(id, -1, -1);
		MedicineSupplier existingmedicineSupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesupplier.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineSupplier != null) {
			existingmedicineSupplier.setId(related_medicinesupplier.getId());
			related_medicinesupplier = existingmedicineSupplier;
		} else {
			related_medicinesupplier = medicineSupplierDAO.store(related_medicinesupplier);
			medicineSupplierDAO.flush();
		}

		medicineprice.setMedicineSupplier(related_medicinesupplier);
		related_medicinesupplier.getMedicinePrices().add(medicineprice);
		medicineprice = medicinePriceDAO.store(medicineprice);
		medicinePriceDAO.flush();

		related_medicinesupplier = medicineSupplierDAO.store(related_medicinesupplier);
		medicineSupplierDAO.flush();

		return medicineprice;
	}
}
